package com.seckill.activemq;

import com.seckill.rabbitmq.MiaoshaMessage;
import com.seckill.redis.RedisService;
import com.seckill.service.GoodsService;
import com.seckill.service.MiaoshaOrderService;
import com.seckill.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @author yjw
 * @date 2022/3/20 17:28
 */
@Slf4j
@Component
public class ActivemqConsumer {

    @Autowired
    RedisService redisService;

    @Autowired
    OrderService orderService;

    @Autowired
    MiaoshaOrderService miaoshaOrderService;

    /**
     * 创建秒杀订单 （秒杀开始->预减库存->mq->创建秒杀订单）
     *
     * @param msg 秒杀信息（用户、商品Id）
     */
    @JmsListener(destination = ActivemqConfig.MIAOSHA_ORDER_TOPIC)
    public void receiveMiaoshaMsg(String msg) {
        log.info("receiveMiaoshaMsg ==================> {}", msg);
        MiaoshaMessage miaoshaMessage = redisService.stringToBean(msg, MiaoshaMessage.class);
        miaoshaOrderService.doMiaoshaOrder(miaoshaMessage);
    }

    /**
     * 订单超时未支付取消
     *
     * @param orderId 订单Id
     */
    @JmsListener(destination = ActivemqConfig.DELAY_ORDER_TOPIC)
    public void receiveScheduleOrderMsg(String orderId) {
        log.info("receive orderId ==================> {}", orderId);
        orderService.orderDelayCancel(Integer.valueOf(orderId));
    }


}